/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author RAI+ruben.arenas
 */
import java.net.*;
import java.io.*;



public class MiServidor extends Thread{
	       MulticastSocket s;
	       String address;
               InetAddress group;
	       int socketNumber;
               boolean terminar;
               boolean leido;
               DatagramPacket lastMessageReceived;
               
               String mensajeOrigen;
               String ipOrigen;
               String nombreEquipoOrigen;

//*************************************************
public MiServidor(String str){
          super(str);
}
//*************************************************
public void run(){
 try {

	       String address = "224.0.0.100";
               InetAddress group = InetAddress.getByName(address);
	       this.socketNumber = 2345;
               s = new MulticastSocket(socketNumber);
               s.joinGroup(group);

               this.terminar = false;
               this.leido = false;
               int tamMensaje = 256;
               
               //Variables que se imprimirán en la GUI
               this.mensajeOrigen="";
               this.ipOrigen="";
               this.nombreEquipoOrigen="";

                
	byte[] buffer = new byte[tamMensaje];
   
    while(!this.terminar){
        if(!this.leido){
		for(int ib=0; ib<tamMensaje; ib++){
			buffer[ib]=' ';
		}

       
               this.lastMessageReceived = new DatagramPacket(buffer, buffer.length);
               
               s.receive(this.lastMessageReceived);
               this.leido=true;
               
               this.mensajeOrigen        = new String(this.lastMessageReceived.getData());
               this.ipOrigen             = this.lastMessageReceived.getAddress().getHostAddress().toString();
               this.nombreEquipoOrigen   = this.lastMessageReceived.getAddress().getHostName().toString();
               
               
	       System.out.println("\n\n<-------------------------------------------");
               System.out.println("Received:" + this.mensajeOrigen);
	       System.out.println("IP origen: " + this.lastMessageReceived.getAddress());
               System.out.println("IP origen: " + this.ipOrigen);
               System.out.println("Nombre: " + this.nombreEquipoOrigen);
               System.out.println("------------------------------------------->");
               
               /*
	       System.out.println("\n\n<-------------------------------------------");
               System.out.println("Received:" + new String(this.lastMessageReceived.getData()));
	       System.out.println("IP origen: " + this.lastMessageReceived.getAddress());
               System.out.println("IP origen: " + this.lastMessageReceived.getAddress().getHostAddress().toString());
               System.out.println("Nombre: " + this.lastMessageReceived.getAddress().getHostName().toString());
               System.out.println("------------------------------------------->");
                */
       }         
     }//fin while  
     s.leaveGroup(group);
     System.out.println("SALIENDO DEL SERVIDOR.");
     
     }
     catch (SocketException e)	{ System.out.println("Socket: " + e.getMessage());  }
     catch (IOException e)	{ System.out.println("IO: " + e.getMessage());      }
     finally 		 	{ if(s != null) s.close();	}

}
//*************************************************

}